//
//  MYLiveGiftSingleton.m
//  JQLive
//
//  Created by Bruce on 2020/6/18.
//  Copyright © 2020 mlqk. All rights reserved.
//

#import "MYLiveGiftSingleton.h"
#import "SDImageCache.h"
//#import "NTESPresent.h"
/*!
 *  账户信息路径
 */
#define AccountFilepath [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"MyGift.data"]
@interface MYLiveGiftSingleton ()

@end
@implementation MYLiveGiftSingleton
//设置单例
+ (MYLiveGiftSingleton *)sharedManager
{
    static MYLiveGiftSingleton *giftManager = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        giftManager = [[MYLiveGiftSingleton alloc] init];
    });
    return giftManager;
}
/**
 存储礼物信息
 
 @param array 礼物数组
 */
- (void)saveAccount:(NSArray *)array
{
    self.giftArray = array;
//    //存储本地图
//    for (int i = 0; i <array.count; i ++)
//    {
//        NSDictionary *dict = array[i];
//        NTESPresent *present = [NTESPresent yy_modelWithDictionary:dict];
//        UIImageView *tempIV = [UIImageView new];
//        [tempIV sd_setImageWithURL:[NSURL URLWithString:present.image] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//
//        }];
//
//    }
    
    
//    if ([NSKeyedArchiver archiveRootObject:array toFile:AccountFilepath]) {
//        NSLog(@"\n\n\n归档成功：路径%@",AccountFilepath);
//    }
//    else
//    {
//        NSLog(@"归档失败");
//    }
}

/**
 清除账户信息
 */
- (void)clearAccount
{
    self.giftArray = nil;
    
    if ([[NSFileManager defaultManager] removeItemAtPath:AccountFilepath error:nil]) {
        NSLog(@"\n\n\n清除归档成功：路径%@",AccountFilepath);
    }
    else
    {
        NSLog(@"清除归档失败");
    }
}



- (NSArray *)giftArray
{
    if (!_giftArray) {
        _giftArray = [NSKeyedUnarchiver unarchiveObjectWithFile:AccountFilepath];

        if (_giftArray) {
               NSLog(@"\n\n\n获取成功：路径%@",AccountFilepath);
           }
           else
           {
               NSLog(@"获取归档失败");
           }
    }
    return _giftArray;

}

@end
